package com.ishowcode.controller.MessageController;

import com.ishowcode.annotation.GlobalInterceptor;
import com.ishowcode.entity.Message;
import com.ishowcode.entity.Result;
import com.ishowcode.entity.User;
import com.ishowcode.service.MessageInfoService;
import com.ishowcode.service.UserInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MessageController {
    @Resource
    UserInfoService userInfoService;
    @Resource
    MessageInfoService messageInfoService;
    @RequestMapping("/getAllMessage")
    @GlobalInterceptor(checkParam = true,checkLogin = true)
    public Result getAllMessage(@RequestParam("email") String email){
        User user = userInfoService.queryUserByEmail(email);
        List<Message> allMessageById = messageInfoService.getAllMessageById(user.getUserId());
        return Result.success(allMessageById);
    }
    @RequestMapping("deleteMessage")
    @GlobalInterceptor(checkParam = true,checkLogin = true)
    public Result deleteMessage(@RequestParam("email") String email,@RequestParam("messageIds")List<String> messageIds){
        if(messageIds.size()<=0)return Result.failure("请勾选要删除的消息");
        for (int i = 0; i < messageIds.size() ; i++) {
            Integer integer = messageInfoService.deleteMessage(messageIds.get(i));
            if (integer<=0) return Result.failure("删除失败");
        }
        return Result.success();
    }
}
